Python has been my go-to language through the years for everything from class projects in college to tiny scripts to help me automate recurring tasks. It's one of few languages out there that is both easy to get started with for beginners yet incredibly powerful when beginners graduate to working on real-world projects.
To edit Python programs, you have a number of options. Some people still prefer a basic text editor, like Emacs, Vim, or Gedit, all of which can be extended with features like syntax highlighting and autocomplete. But a lot of power users working on large projects with complex code bases prefer an integrated development environment (IDE) to the text editor plus terminal combination. The line between an advanced text editor and a slim IDE isn't always clear, and we'll leave it up to you to decide exactly which features you require for your development needs.
Python Development-specific IDEs
PyCharm is a Python-specific IDE built on JetBrains' platform. There are free editions for students and open-source projects.
Wing IDE is a paid development environment with integrated debugging and code completion.
PyDev is a Python IDE plugin for Eclipse.
In the past couple of years, several cloud-based development environments have popped up. These can work great for when you're learning or stuck on a machine with only a browser but no way to install your own software. Most of these have free tiers for getting started and paid tiers as you scale up your application.
Inclined to build a profession as Python Developer? Then here is the blog post on, explore Python Training
Python is easy to learn and use. The use of Python for web development is gradually but surely increasing as suggested by Google Trends.
Python web development is a huge topic. There are a number of ways how we can proceed and it’s something that cannot be explained in a single article. Instead, we will learn how to connect a database (MySQL) in Python, a beginning step towards Python web development. There is a number of ways to connect MySQL using Python. Some of them are:
MySQLdb: a thread-compatible interface to MySQL
Mysql-connector-python: MySQL driver is written in Python
PyMySQL: a pure-Python MySQL driver
In this article, we will use PyMySQL to connect MySQL using Python. The benefits of using PyMySQL will be discussed later in the article once we learn how to use it.
On Windows: python -m pip install -U pip setup tools
On Linux or macOS: pip install -U pip setup tools
To install pyMySQL, simply issue the following command: pip install PyMySQL
Create a database using MySQL Workbench, phpMyAdmin or any other tool you prefer. We will create python_tekslate database for our use.
Import pymysql and cursors using
import pymysql.cursors import pymysql
Here, pymysql.cursor imports object to interact with the MySQL database.
To connect to our database,
connection = pymysql.connect( host = 'localhost', user = 'root', password = 'password', db = 'python_tekslate', charset = 'utf8', cursorclass = pymysql.cursors.DictCursor)
Note: You may need to modify the above code (user, password, db etc.) to work.
For now, we will simply create a user table with a column name.
sql = "CREATE TABLE IF NOT EXISTS users (name VARCHAR(100) NOT NULL PRIMARY KEY)" cursor = connection.cursor() execute(sql)
Here, we created a sql variable to put SQL query. Then, the query is executed.
Then the connection is committed to take effect.
commit()
Finally, close the cursor.
close()
Here is the complete code to create a new table in our database:
import pymysql.cursors import pymysql connection = pymysql.connect( host = 'localhost', user = 'root', password = 'admin', db = 'test', charset = 'utf8', cursorclass = pymysql.cursors.DictCursor) sql = "CREATE TABLE IF NOT EXISTS users (name VARCHAR(100) NOT NULL PRIMARY KEY)" cursor = connection.cursor() cursor.execute(sql) connection.commit() cursor.close()
Here are a few more examples:
import pymysql.cursors import pymysql connection = pymysql.connect( host = 'localhost', user = 'root', password = 'admin', db = 'test', charset = 'utf8', cursorclass = pymysql.cursors.DictCursor) try: with connection.cursor() as cursor: # Read all record from users table sql = "SELECT name FROM users" cursor.execute(sql) result = cursor.fetchall() print(result) finally: connection.close()
Suppose, the user table has two rows of data Bill and Jack. Then, you will get the following output:
[{'name': Bill}, {'name': 'Jack'}]
Now, you can easily manipulate the result however you wish. If the table is empty, None object is returned. Here, we have used Python try…finally statement for safety. If there is a problem with executing our code, the connection is closed. Also, the cursor (created by the connection.cursor()) is closed when there is a problem with the cursor. We have used with statement to accomplish this task.
import pymysql.cursors import pymysql connection = pymysql.connect( host = 'localhost', user = 'root', password = 'admin', db = 'test', charset = 'utf8', cursorclass = pymysql.cursors.DictCursor) try: with connection.cursor() as cursor: # New record is created in users table sql = "INSERT INTO users (`name`) VALUES (%s);" cursor.execute(sql, 'Bill') connection.commit() finally: connection.close()
Here, Bill is inserted to the name column of users table.
Note: You need to commit the connection manually while inserting data.
Not only pyMySQL allows you to connect to the database, it can be used to create the database as well.
import pymysql.cursors import pymysql connection = pymysql.connect( host = 'localhost', user = 'root', password = 'admin', charset = 'utf8', cursorclass = pymysql.cursors.DictCursor) cursor = connection.cursor() # database name database = 'node' cursor.execute("CREATE DATABASE IF NOT EXISTS " + database) connection.commit() cursor.close()
We created a new database node in the above code. If the database already exists, it will remain unchanged.
Also, I would suggest you learn Python’s SQLAlchemy. It’s an open-source toolkit and ORM (Open-Relational-Mapper) for Python programming. You can avoid writing error-prone raw SQL statements that are hard to maintain.
For an In-depth knowledge on Python, click on below
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.